package org.example.alicloud.handler;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import org.example.alicloud.model.CommonResult;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * 自定义限流处理
 * 注意降级、限流的处理方法
 * 返回值必须与原方法一致，
 * 附加一个Exception或者Throwable参数
 * 如果定义在处理类中，必须使用static定义
 * @author gaoqiang
 */
public class CustomerBlockHandler {
    public static CommonResult handlerException(BlockException exception){
        return new CommonResult(444,"按客户自定义限流处理,global handlerException");
    }

    public static CommonResult handlerException2(BlockException exception){
        return new CommonResult(444,"按客户自定义限流处理2,global handlerException2");
    }

    public static String handlerDegradeException2(Throwable throwable){
        return "按客户自定义降级异常处理,global handler for DegradeException";
    }

    // 下面时测试blockHandler和fallbackHandler用到的方法，测试结果同时配置，只有fallback生效。

    public static CommonResult handleBlockException(@PathVariable("id") Long id, BlockException exception){
        return new CommonResult(444,"按客户自定义block处理,handle block exception");
    }

    public static CommonResult handleGlobalFallbackException(@PathVariable("id") Long id, Throwable throwable){
        return new CommonResult(444,"按客户自定义全局fallback处理,global fallback handler for Exception");
    }

    public static CommonResult handleFallbackException(@PathVariable("id") Long id, Throwable throwable){
        return new CommonResult(444,"按客户自定义fallback处理,fallback handler for Exception");
    }


}
